package com.linkin.homepage.base.recycleview;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public class RecyclerViewUtils {

    public static List getVisibleList(RecyclerView recyclerView) {
        List visibleList = new ArrayList();
        BaseRCAdapter adapter = getAdapter(recyclerView);
        if (adapter != null) {
            int first = getFirstVisiblePosition(recyclerView);
            int last = getLastVisiblePosition(recyclerView);
            if (first <= last && first >= 0) {
                List list = adapter.getList();
                for (int i = first; i <= last; i++) {
                    Object o = list.get(i);
                    if (o != null) {
                        visibleList.add(o);
                    }
                }
            }
        }
        return visibleList;
    }

    public static BaseRCAdapter getAdapter(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof BaseRCAdapter) {
                return (BaseRCAdapter) adapter;
            }
            if (adapter instanceof RCWrapperAdapter) {
                RecyclerView.Adapter innerAdapter =
                        ((RCWrapperAdapter) adapter).getInnerAdapter();
                if (innerAdapter instanceof BaseRCAdapter) {
                    return (BaseRCAdapter) innerAdapter;
                }
            }
        }
        return null;
    }

    public static int getFirstVisiblePosition(RecyclerView recyclerView) {
        View firstChild = recyclerView.getChildAt(0);
        int position = recyclerView.getChildLayoutPosition(firstChild);
        if (position >= 0) {
            BaseRCAdapter adapter = getAdapter(recyclerView);
            if (adapter != null) {
                return Math.min(position, adapter.getItemCount() - 1);
            }
        }
        return -1;
    }

    public static int getLastVisiblePosition(RecyclerView recyclerView) {
        View lastChild = recyclerView.getChildAt(recyclerView.getChildCount() - 1);
        int position = recyclerView.getChildLayoutPosition(lastChild);
        if (position >= 0) {
            BaseRCAdapter adapter = getAdapter(recyclerView);
            if (adapter != null) {
                return Math.min(position, adapter.getItemCount() - 1);
            }
        }
        return -1;
    }

    public static void setHeaderView(RecyclerView recyclerView, View view) {
        if (recyclerView != null && view != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                RCWrapperAdapter wrapperAdapter = (RCWrapperAdapter) adapter;
                wrapperAdapter.setHeaderView(view);
            }
        }
    }

    public static View getHeaderView(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                RCWrapperAdapter wrapperAdapter = (RCWrapperAdapter) adapter;
                return wrapperAdapter.getHeaderView();
            }
        }
        return null;
    }

    public static void removeHeaderView(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                RCWrapperAdapter wrapperAdapter = (RCWrapperAdapter) adapter;
                View headerView = wrapperAdapter.getHeaderView();
                if (headerView != null) {
                    wrapperAdapter.removeHeaderView(headerView);
                }
            }
        }
    }

    public static void setFooterView(RecyclerView recyclerView, View view) {
        if (recyclerView != null && view != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                RCWrapperAdapter wrapperAdapter = (RCWrapperAdapter) adapter;
                wrapperAdapter.setFooterView(view);
            }
        }
    }

    public static View getFooterView(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                RCWrapperAdapter wrapperAdapter = (RCWrapperAdapter) adapter;
                return wrapperAdapter.getFooterView();
            }
        }
        return null;
    }

    public static void removeFooterView(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                RCWrapperAdapter wrapperAdapter = (RCWrapperAdapter) adapter;
                View footerView = wrapperAdapter.getFooterView();
                if (footerView != null) {
                    wrapperAdapter.removeFooterView(footerView);
                }
            }
        }
    }

    public static int getLayoutPosition(RecyclerView recyclerView, RecyclerView.ViewHolder holder) {
        if (recyclerView != null && holder != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                int headerCount = ((RCWrapperAdapter) adapter).getHeaderViewsCount();
                if (headerCount > 0) {
                    return holder.getLayoutPosition() - headerCount;
                }
            }
        }
        return holder != null ? holder.getLayoutPosition() : RecyclerView.NO_POSITION;
    }

    public static int getAdapterPosition(RecyclerView recyclerView, RecyclerView.ViewHolder holder) {
        if (recyclerView != null && holder != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                int headerCount = ((RCWrapperAdapter) adapter).getHeaderViewsCount();
                if (headerCount > 0) {
                    return holder.getAdapterPosition() - headerCount;
                }
            }
        }
        return holder != null ? holder.getAdapterPosition() : RecyclerView.NO_POSITION;
    }

    public static int getItemCount(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                RecyclerView.Adapter innerAdapter = ((RCWrapperAdapter) adapter).getInnerAdapter();
                return innerAdapter != null ? innerAdapter.getItemCount() : 0;
            }
            return adapter != null ? adapter.getItemCount() : 0;
        }
        return 0;
    }

    public static int getHeaderViewCount(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                return ((RCWrapperAdapter) adapter).getHeaderViewsCount();
            }
        }
        return 0;
    }

    public static int getFooterViewCount(RecyclerView recyclerView) {
        if (recyclerView != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof RCWrapperAdapter) {
                return ((RCWrapperAdapter) adapter).getFooterViewsCount();
            }
        }
        return 0;
    }
}